package cn.com.egova.materialdesignexample.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import cn.com.egova.materialdesignexample.R;
import cn.com.egova.materialdesignexample.view.RoundImageView;

public class NavigationViewActivity extends AppCompatActivity implements View.OnClickListener,NavigationView.OnNavigationItemSelectedListener{
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private RoundImageView roundImageView;
    private Menu mMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_view);

        initToolbar();

        initView();

        initListener();
    }

    private void initToolbar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("NavigationView使用");
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new NavigationView.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initView(){
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView = (NavigationView) findViewById(R.id.navigationView);
        navigationView.setItemIconTintList(null);
        //java代码中一定要通过下面的方式获取头部布局.
        LinearLayout ll = (LinearLayout) navigationView.getHeaderView(0);
        roundImageView = (RoundImageView) ll.findViewById(R.id.header);

        //获取navigationView的菜单资源.
        mMenu = navigationView.getMenu();
    }


    private void initListener(){
        roundImageView.setOnClickListener(this);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case  R.id.personal:
                if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }else{
                    drawerLayout.openDrawer(navigationView);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.header:
                Toast.makeText(this,"点击的是头像!",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        String text = "";
        switch (item.getItemId()) {
            case  R.id.drawer_home:

                break;
            case  R.id.drawer_favourite:

                break;
            case  R.id.drawer_downloaded:

                break;
            case  R.id.drawer_more:

                break;
            case  R.id.drawer_settings:

                break;
            case  R.id.drawer_author:

                break;
        }

        resetMenuCheckdState(mMenu);
        item.setChecked(true);
        text = item.getTitle().toString();
        Toast.makeText(this,"点击的是:"+text,Toast.LENGTH_SHORT).show();
        return false;
    }


    /**
     * 重置所有菜单项的选中状态为false;
     * @param menu
     */
    private void resetMenuCheckdState(Menu menu){
        //发现两个问题:
        //1.menu.size()方法获取的是menu的以及菜单的个数,如本例就是4个,group只算1个.
        //2.二级菜单无法响应setChecked(),不管java代码设置还是xml中设置都不管用.
//        int size = mMenu.size();
//        for(int i=0;i<size;i++){
//            MenuItem menuItem = menu.getItem(i);
//            boolean state = item==menuItem?true:false;
//            item.setChecked(state);
//        }

//        MenuItem home = menu.findItem(R.id.drawer_home).setChecked(false);
//        menu.findItem(R.id.drawer_favourite).setChecked(false);
//        menu.findItem(R.id.drawer_downloaded).setChecked(false);
//        menu.findItem(R.id.drawer_more).setChecked(false);
//        menu.findItem(R.id.drawer_settings).setChecked(false);
//        menu.findItem(R.id.drawer_author).setChecked(false);
    }
}
